package com.sinovatech.ologger.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class Misc {

	public static final int SEVENDAYMINITES = 1 * 24 * 60;
	public static final String CLIENT_INVALID_DATE = "2099-12-31";
	private static final String[] alphabet = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
			"w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

	private static final String[] nums = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
	private static final String LANG_ZH = "zh";
	private static final String[] Locale_Changes_For_ZH = { "12:", "0:", "下", "中" };

	public static String generateRandomNumber(int length) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			sb.append(nums[(int) (Math.random() * 10)]);
		}
		return sb.toString();
	}

	public static String generateRandomAlphabet() {
		return new StringBuffer().append("").append(alphabet[(int) (Math.random() * 26)]).append(alphabet[(int) (Math.random() * 26)]).append(
				alphabet[(int) (Math.random() * 26)]).append(alphabet[(int) (Math.random() * 26)]).append(alphabet[(int) (Math.random() * 26)]).append(
				alphabet[(int) (Math.random() * 26)]).toString();
	}

	public static Timestamp getMinTimestamp() {
		Timestamp ts = Timestamp.valueOf("1930-01-01 00:00:00");
		return ts;
	}

	public static String offSetDate(final String date, int days) {

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String[] myDay = date.split("-");
		GregorianCalendar greCalendear = new GregorianCalendar();
		greCalendear.set(Calendar.YEAR, Integer.parseInt(myDay[0]));
		greCalendear.set(Calendar.MONTH, Integer.parseInt(myDay[1]) - 1);
		greCalendear.set(Calendar.DAY_OF_MONTH, Integer.parseInt(myDay[2]));
		System.out.println(df.format(greCalendear.getTime()));
		int day = greCalendear.get(Calendar.DAY_OF_MONTH);
		day += days;
		greCalendear.set(Calendar.DAY_OF_MONTH, day);
		return df.format(greCalendear.getTime());
	}

	public static Timestamp stringToTimestamp(String time) {
		try {
			Timestamp ts = Timestamp.valueOf(time);
			return ts;
		} catch (Exception e) {
			return Misc.getMinTimestamp();
		}
	}

	public static String getDate() {
		String temp = getSystemDateTime();
		int index = temp.indexOf(" ");
		temp = temp.substring(0, index);
		return temp;

	}

	public static String getSystemDateTime() {
		Calendar cal = Calendar.getInstance();
		StringBuffer sb = new StringBuffer();
		sb.append(cal.get(Calendar.YEAR));
		sb.append("-");
		sb.append(cal.get(Calendar.MONTH) + 1);
		sb.append("-");
		sb.append(cal.get(Calendar.DAY_OF_MONTH));
		sb.append(" ");
		sb.append(cal.get(Calendar.HOUR_OF_DAY));
		sb.append(":");
		sb.append(cal.get(Calendar.MINUTE));
		sb.append(":");
		sb.append(cal.get(Calendar.SECOND));
		return sb.toString();
	}

	public static String getSystemDateTimeForYMD() {
		Calendar cal = Calendar.getInstance();
		StringBuffer sb = new StringBuffer();
		sb.append(cal.get(Calendar.YEAR));
		sb.append("-");
		if (cal.get(Calendar.MONTH) + 1 < 10) {
			sb.append("0");
		}
		sb.append(cal.get(Calendar.MONTH) + 1);
		sb.append("-");
		if (cal.get(Calendar.DAY_OF_MONTH) < 10) {
			sb.append("0");
		}
		sb.append(cal.get(Calendar.DAY_OF_MONTH));
		return sb.toString();
	}

	public static Timestamp getCurrentTimestamp() {
		Calendar cal = Calendar.getInstance();
		Timestamp ts = new Timestamp(cal.getTimeInMillis());
		return ts;
	}

	public static String getDateTimeByLocale(Timestamp time, Locale locale) {
		if (time == null)
			return null;
		if (locale == null) {
			locale = Locale.CHINA;
		}
		DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT, locale);
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(time.getTime());
		String result = df.format(cal.getTime());
		if (locale.getLanguage().equalsIgnoreCase(LANG_ZH)) {
			int hour = cal.get(Calendar.HOUR_OF_DAY);
			if (hour == 0) {
				result = result.replaceFirst(Locale_Changes_For_ZH[0], Locale_Changes_For_ZH[1]);
			} else if (hour == 12) {
				result = result.replaceFirst(Locale_Changes_For_ZH[2], Locale_Changes_For_ZH[3]);
			}
		}
		return result;
	}

	public static String getShortDateTimeByLocale(Date time, Locale locale) {
		if (time == null)
			return null;
		if (locale == null) {
			locale = Locale.CHINA;
		}
		DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, locale);
		return df.format(time);
	}

	/**
	 * 根据传入时间和分钟数返回新的timestamp
	 * @param timestamp 当前timestamp
	 * @param min 分钟数 可以为负数，表示减去
	 * @return
	 */
	public static Timestamp getTimestampForAddMin(Timestamp timestamp, int min) {
		if (timestamp == null)
			return null;
		long time2 = 0;
		long time = timestamp.getTime();
		if (min < 0) {
			time2 = time - (Math.abs(min) * 60 * 1000L);
		} else {
			time2 = time + (min * 60 * 1000L);
		}
		Timestamp result = new Timestamp(time2);
		return result;
	}
}
